package myky.com.doctorapp.ui

import android.content.Context
import android.graphics.Color
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.util.Log
import android.util.TypedValue
import android.view.View
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_health_record_home_page.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.ViewPagerAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.base.BaseFragment
import myky.com.doctorapp.contract.NullContract
import myky.com.doctorapp.fragment.PersionHabbitsFragment
import myky.com.doctorapp.fragment.PersionInfoFragment
import myky.com.doctorapp.fragment.PersionalDiseaseHistorykFragment
import myky.com.doctorapp.presenter.NullPresenter
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

class HealthRecordHomePageActivity : BaseActivity<NullPresenter,String>(),NullContract.View {


    lateinit var fragments:ArrayList<Fragment>

    lateinit var tabtitles:ArrayList<String>




    override fun showDataSuccess(datas: String?) {
    }


    override fun reloadActivity() {
    }

    override fun injectTarget(): View =findViewById(R.id.activity_health_record)

    override fun initData() {



    }

    override fun initView() {

        conmonTitleTextView.text="健康档案"

        ImmersionBar.with(this).barColorInt(ContextCompat.getColor(this, R.color.colorPrimary)).titleBar(R.id.commonTitleLayout).navigationBarEnable(false).init()


        initFragment()

        initTabs()


        backLayout.setOnClickListener { finish() }
    }

    private fun initTabs() {

        magicIndicator.setBackgroundColor(Color.parseColor("#ffffff"))
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {

                return tabtitles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = ColorTransitionPagerTitleView(context)
                simplePagerTitleView.text = tabtitles.get(index)
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
                simplePagerTitleView.normalColor = Color.parseColor("#333333")
                simplePagerTitleView.selectedColor = resources.getColor(R.color.theme_app)
                simplePagerTitleView.setOnClickListener {
                    Log.i("out", "点击。。$index")
                    viewPager.currentItem = index
                }

                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                indicator.xOffset = -15f

                indicator.setColors(resources.getColor(R.color.theme_app))
                return indicator
            }


        }

        magicIndicator.navigator = commonNavigator


        val manager = supportFragmentManager
        viewPager.offscreenPageLimit = 3
        viewPager.adapter = ViewPagerAdapter(manager, fragments)
        viewPager.currentItem = 0
        ViewPagerHelper.bind(magicIndicator, viewPager)



    }

    private fun initFragment() {
        fragments=ArrayList()

        fragments.add(PersionInfoFragment())
        fragments.add(PersionalDiseaseHistorykFragment())
        fragments.add(PersionHabbitsFragment())


        tabtitles=ArrayList()

        tabtitles.add("个人信息")
        tabtitles.add("以往病史")
        tabtitles.add("生活习惯")


    }

    override fun getContenView(): Int =R.layout.activity_health_record_home_page
}
